import {Component} from '@angular/core';
import {IonicPage, ToastController} from 'ionic-angular';

import {LocalstorageProvider} from '../../providers/localstorage/localstorage'
import {HttphandleProvider} from '../../providers/httphandle/httphandle'
/**
 * Generated class for the SettingsPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */
import {Router} from"@angular/router";
import { Tools } from '../../tools/tools';
import { NativePageTransitions } from '@ionic-native/native-page-transitions';
import { Network } from '@ionic-native/network';

@IonicPage()
@Component({
  selector: 'page-modifyPassword',
  templateUrl: 'modify-password.html',
})

export class ModifyPasswordPage extends Tools{
  private disabled: boolean = true;
  private modifyObj: any;
  private updatePassword: any;
  private userId:string;

  constructor(
    protected route: Router,
    private loc: LocalstorageProvider,
    private http: HttphandleProvider,
    private toastCtrl: ToastController,
    protected nativePageTransitions: NativePageTransitions,
    protected network: Network
  ) {
    super(route, nativePageTransitions, network)
    this.modifyObj = {
      password: "",
      newPassword: "",
      againPassword: "",
    };

    this.userId = this.loc.get('userinfo').userID;
  }

  ionViewDidLoad() {
    console.log('这里是修改密码页面');
  }

  txtChange() {
    if (this.modifyObj) {
      if (this.modifyObj.password !== "" && this.modifyObj.newPassword !== "" && this.modifyObj.againPassword !== "") {
        if (this.modifyObj.newPassword.length >= 6 && this.modifyObj.againPassword.length >= 6) {
          this.disabled = false
        }
      } else {
        this.disabled = true
      }
    }
  }

  back() {
    this.openPage('accountNumber')
  }

  updatePassWord() {
    this.updatePassword = {
      userId: this.userId,
      oldPassword: this.modifyObj.password,
      newPassword: this.modifyObj.newPassword,
    }
    if (this.modifyObj) {
      if (this.modifyObj.password !== "" && this.modifyObj.newPassword !== "" && this.modifyObj.againPassword !== "") {
        if (this.modifyObj.password === this.modifyObj.newPassword) {
          alert("当前 密码与新密码不能相同")
        } else {
          if (this.modifyObj.againPassword === this.modifyObj.newPassword) {
            this.http.post("/app/rest/v2/services/yes_UserService/resetUserPassword", this.updatePassword).then(res => {
              // alert("修改密码" + res.message)
              if (res.flag === true) {
                alert('修改密码成功');
                this.openPage('login')
                this.loc.remove("token");
              } else {
                alert(res.message)
              }
            }).catch(err => {
              if (err.status === 401) {
                this.http.getToken((res) => {
                  this.loc.set('token', res.json())
                  this.updatePassWord()
                })
              } else {
                this.toastCtrl.create({
                  message: err.errMsg || err,
                  position: 'top',
                  duration: 1000
                }).present()
                console.log("服务器或网络", err)
              }
            })
          } else {
            alert("请输入相同的新密码")
          }
        }
      }
    }
  }
}
